package com.easyattendance.services;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.easyattendance.model.Assignment;
import com.easyattendance.model.AssignmentStudent;
import com.easyattendance.model.Class;
import com.easyattendance.model.Material;
import com.easyattendance.model.Section;
import com.easyattendance.model.Student;
import com.easyattendance.model.Teacher;
import com.easyattendance.util.HibernateUtil;

public class AddAssignmentService {
		
	/* Method to add assignment record in the database */
	   public synchronized boolean addAssignment(Assignment assignment){
	      Session session = HibernateUtil.openSession();
	      Transaction tx = null;
	      boolean result=false;
	      
	      try{
	         tx = session.beginTransaction();

	         session.save(assignment);
	        	 
	         tx.commit();
	         result=true;
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
	     return result;
	   }
	   
	   /* Method to DELETE an assignment from the records */
	   public synchronized boolean deleteAssignment(String assignmentId,String classId,String subjectId){
		   Session session = HibernateUtil.openSession();
	      Transaction tx = null;
	      boolean flag = false;
	      try{
	         tx = session.beginTransaction();
	        
	         Assignment assignment = (Assignment) session.createQuery("FROM Assignment where assignment_id='"+assignmentId+"' and class_id='"+classId+"' and subject_id='"+subjectId+"' ").uniqueResult();
	         session.delete(assignment);
	         
	         tx.commit();
	         flag = true;
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
	      return flag;
	   }
	   
	   /* Method to DELETE an student assignment records */
	   public synchronized boolean deleteAssignmentStudent(String assignmentId,String classId,String subjectId){
		   Session session = HibernateUtil.openSession();
	      Transaction tx = null;
	      List<AssignmentStudent> AssignmentStudList=null; 
		 
	      boolean flag = false;
	      try{
	         tx = session.beginTransaction();
	        
	         AssignmentStudList=session.createQuery("FROM AssignmentStudent where assignment_id='"+assignmentId+"' and class_id='"+classId+"' and subject_id='"+subjectId+"' ").list();
	         
	         for(Iterator iterator=AssignmentStudList.iterator();iterator.hasNext();)
	         {
	        	 AssignmentStudent assignment_stud = (AssignmentStudent) iterator.next();
		         session.delete(assignment_stud);
	         }
	         
	         tx.commit();
	         flag = true;
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
	      return flag;
	   }
	   
	   public boolean ifAlreadyExists(Assignment assignment) {
			 boolean flag = false;
			 
			 Session session = HibernateUtil.openSession();
		      Transaction tx = null;
		      List assignmentList = null;
		      try{
		         tx = session.beginTransaction();
		         assignmentList = session.createQuery("FROM Assignment").list(); 
		         for (Iterator iterator = assignmentList.iterator(); iterator.hasNext();){
		        	 Assignment tempAssignment = (Assignment) iterator.next(); 
		            if(tempAssignment.equals(assignment)) {
		            	System.out.println("Assignment already uploaded");
		            	flag = true;
		            	break;
		            }
		             
		         }
		         tx.commit();
		      }catch (HibernateException e) {
		         if (tx!=null) tx.rollback();
		         e.printStackTrace(); 
		      }finally {
		         session.close(); 
		      }
			 
			 return flag;
		}
	   
	   /* Method to  READ all the assignment records */
	   public List listAssignments( ) {
		   Session session = HibernateUtil.openSession();
	      Transaction tx = null;
	      List assignments = null;
	      try{
	         tx = session.beginTransaction();
	         assignments = session.createQuery("FROM Assignment").list(); 
	         for (Iterator iterator =assignments.iterator(); iterator.hasNext();){
	        	Assignment tempassignment = (Assignment) iterator.next(); 
	            System.out.print("AssignmentId: " + tempassignment.getAssignment_id()); 
	             
	         }
	         tx.commit();
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
	      return assignments;
	   }
	   
	   /* Method to  READ all the assignment records given by particular teacher */
	   public List<Assignment> listAssignments(String currentUser) {
		   Session session = HibernateUtil.openSession();
	      Transaction tx = null;
	      List<Assignment> assignments = null;
	      try{
	         tx = session.beginTransaction();
	         Query query = session.createQuery("from Teacher where email_id='"+currentUser+"'");
	         Teacher teacher = (Teacher)query.uniqueResult();
	         assignments = session.createQuery("FROM Assignment where teacher_id='"+teacher.getTeacher_id()+"'").list(); 
	         for (Iterator iterator = assignments.iterator(); iterator.hasNext();){
	        	Assignment tempassignment = (Assignment) iterator.next(); 
	            System.out.print("AssignmentId: " + tempassignment.getAssignment_id()); 
	             
	         }
	         tx.commit();
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
	      return assignments;
	   }
	   
	   /* Method to  READ all the assignment records given for particular subject */
	   public List<Assignment> listAssignmentsBySubject(String subjectId,String classId) {
		   Session session = HibernateUtil.openSession();
	      Transaction tx = null;
	      List<Assignment> assignments = null;
	      try{
	         tx = session.beginTransaction();
	        
	         assignments = session.createQuery("FROM Assignment where subject_id='"+subjectId+"' and class_id='"+classId+"'").list(); 
	         for (Iterator iterator = assignments.iterator(); iterator.hasNext();){
	        	Assignment tempassignment = (Assignment) iterator.next(); 
	            System.out.print("AssignmentId: " + tempassignment.getAssignment_id()); 
	             
	         }
	         tx.commit();
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
	      return assignments;
	   }
	   
	   /* Method to  get assignment id by assignment name for particular class and subject */
	   public String getAssignmentIdByName(String assignmentName,String subjectId,String classId) {
		  Session session = HibernateUtil.openSession();
	      Transaction tx = null;
	      Assignment assignment = null;
	      //List<Assignment> assignments = null;
	      try{
	         tx = session.beginTransaction();
	        
	         Query query = session.createQuery("FROM Assignment where subject_id='"+subjectId+"' and class_id='"+classId+"' and assignment_name='"+assignmentName+"'"); 
	         assignment=(Assignment) query.uniqueResult();
	             
	         
	         tx.commit();
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
	      return assignment.getAssignment_id();
	   }
	   
	   public ArrayList<Student> getStudentList(String classId) {
			Session session = HibernateUtil.openSession();
			Transaction tx = null;
			ArrayList<Student> studentList=new ArrayList<Student>();

			try {
				tx = session.beginTransaction();
				List<Student> studList = session.createQuery("FROM Student WHERE class_id='"+classId+"'").list();
				
				for(Iterator iterator =studList.iterator(); iterator.hasNext();)
				{
					 Student stud=(Student)iterator.next();
					 int stud_rollno=stud.getStud_roll_no();
					 String stud_fnm=stud.getStud_fname();
					 String stud_lnm=stud.getStud_lname();
					 studentList.add(new Student(stud_rollno,stud_fnm,stud_lnm));
				}
				
				tx.commit();

			} catch (HibernateException e) {
				if (tx != null)
					tx.rollback();
				e.printStackTrace();
			} finally {
				session.close();
			}
			return studentList;
		}

}
